module Table

# get the ansi code for a color string, color
def get_color ground,color
  cols = {
           :fore=>{
                   :red=>31,
                   :black=>30,
                   :green=>32,
                   :yellow=>33,
                   :blue=>34,
                   :magenta=>35,
                   :cyan=>36,
                   :white=>37,
                   :default=>39
                  },
           :back=>{
                   :red=>41,
                   :black=>40,
                   :green=>42,
                   :yellow=>43,
                   :blue=>44,
                   :magenta=>45,
                   :cyan=>46,
                   :white=>47,
                   :default=>49
                  },
         }
  cols[ground][color]
end

# get the english string of ansi code number
# if bf != nil fore and background will be denoted
def get_color_num num,bf=nil
  cols = {
           :fore=>{
                   :red=>31,
                   :black=>30,
                   :green=>32,
                   :yellow=>33,
                   :blue=>34,
                   :magenta=>35,
                   :cyan=>36,
                   :white=>37,
                   :default=>39
                  },
           :font=>{
                    :bold=>1,
                    :underline=>4,
                    :strike=>9,
                    :italic=>3
                  },
           :back=>{
                   :red=>41,
                   :black=>40,
                   :green=>42,
                   :yellow=>43,
                   :blue=>44,
                   :magenta=>45,
                   :cyan=>46,
                   :white=>47,
                   :default=>49
                  }
         }
  col = cols[:fore].find {|x| x[1]==num}
  if col and bf
    col = ["fore:#{col[0]}"]
  end
  unless col
      col = cols[:back].find {|x| x[1]==num}
      if col and bf
        col = ["back:#{col[0]}"]
      end
  end
  unless col
      col = cols[:font].find {|x| x[1]==num}
  end
  return col[0].to_s
end

# create ansi format and Spreadsheet::Format for a cell
# *opts => see Table::RTable.color_xy() or smart_format()
# format => if a Spreadsheet::Format will modify instead of create
# Returns array of [ansi_code,Spreadsheet::Format]
def ansi format=nil,*opts
  code = []
  excel = []
# p opts;exit
  opts.each {|o|
    case o
      when /clear/
        code << "\e[0m"	
        # reset; clears all colors and styles (to white on black)
      when /bold/
        code << "\e[1m"
        excel << ":weight => :bold"
        if format
          format.font.bold=true
        end
        # bold on (see below)
      when /italic/
        code << "\e[3m" 
        excel << ":italic => true"
        if format
          format.font.italic=true
        end
        # italics on
      when /underline/
        code << "\e[4m"
        excel << ":underline => true"
        if format
          format.font.underline=true
        end
        # underline on
      when /inverse/
        code << "\e[7m"
        # inverse on; reverses foreground & background colors
      when /strike/
        code << "\e[9m"
        excel << ":strikeout => true"
        if format
          format.font.strikeout=true
        end
        # strikethrough on
     when /fore:(.*)/
       code << "\e[#{get_color(:fore,$1.to_sym)}m"
       excel << ":color => :#{$1}"
        if format
          format.font.instance_variable_set "@colors", $1.to_sym
        end
       # set foreground color to 
     when /back:(.*)/
       code << "\e[#{get_color(:back,$1.to_sym)}m"
       # set background color to 
    end
  }
  code = code.join
  code
  excel = excel.join(",")
  if excel != [] and format == nil
    excel = eval("Spreadsheet::Format.new(#{excel})")
  end
  excel = format if format
  [code,excel]
end;end